﻿@{
    var grid = Html.DevExpress().GridView(
    settings =>
    {
        settings.Name = "gvSlide";
        settings.KeyFieldName = "ID";
        settings.CallbackRouteValues = new { Controller = "Slide", Action = "SlideRowPartial" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Slide", Action = "UpdateSlide" };
        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Slide", Action = "AddSlide" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Slide", Action = "DeleteSlide" };
        settings.CustomBindingRouteValuesCollection.Add(GridViewOperationType.Paging, new { Controller = "Slide", Action = "SlidePagingRowPartial" });
        settings.CustomBindingRouteValuesCollection.Add(GridViewOperationType.Filtering, new { Controller = "Slide", Action = "SlideFilteringRowPartial" });
        settings.CustomBindingRouteValuesCollection.Add(GridViewOperationType.Sorting, new { Controller = "Slide", Action = "SlideSortingRowPartial" });
        settings.CustomActionRouteValues = new { Controller = "Slide", Action = "ApplyFilterExpressionPartial" };

        settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
        settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.Modal = true;
        settings.CommandColumn.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        settings.SettingsBehavior.ConfirmDelete = true;
        settings.SettingsPopup.EditForm.Width = new System.Web.UI.WebControls.Unit(900);
        settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        settings.SettingsText.PopupEditFormCaption = "Slide Management";
        settings.ControlStyle.CssClass = "edit-form";
        settings.SettingsPager.PageSize = Session["PageSize"] != null ? (int)Session["PageSize"] : 10;
        settings.SettingsBehavior.AllowFocusedRow = true;
        settings.CommandColumn.Visible = true;
        settings.CommandColumn.ShowSelectCheckbox = true;
        settings.ClientSideEvents.Init = @"function(s,e){ 
            s.UnselectAllRowsOnPage();
        }";
        settings.ClientSideEvents.BeginCallback = "gvSlideBeginCallback";
        settings.SetEditFormTemplateContent(e =>
        {
            var model = ViewData["SlideViewModel"] as SlideViewModel;
            if (model == null)
            {
                var slideID = DataBinder.Eval(e.DataItem, "ID");
                if (slideID != null)
                {
                    var slide = new ThinhTien.Data.Repository.SlideRepository();
                    model = slide.GetSlideDetail(new Guid(slideID.ToString()));
                }
                else {
                    model = new SlideViewModel();
                }
            }

            Html.RenderPartial("SlideFormPartial", model);
        });
        
        settings.Columns.Add("Title");
        settings.Columns.Add("Link");
        settings.Columns.Add("LookupName");

        settings.Columns.Add(column =>
        {
            column.FieldName = "UpdatedDate";
            column.Caption = "Updated Date";
            column.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "UpdatedBy";
            column.Caption = "Updated By";
            column.Settings.AutoFilterCondition = AutoFilterCondition.Contains;
        });
        
        var commandColumn = settings.Columns.Add("Action", "Action");
        commandColumn.Settings.AllowAutoFilter = DefaultBoolean.False;
        commandColumn.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        commandColumn.Width = 1;
        commandColumn.SetDataItemTemplateContent(c =>
        {
            Html.DevExpress().Button(s =>
            {
                s.Name = string.Format("btnEdit_{0}", DataBinder.Eval(c.DataItem, "ID"));
                s.Text = "Edit";
                s.ClientSideEvents.Click = "function(s,e){ gvSlide.StartEditRowByKey('" + DataBinder.Eval(c.DataItem, "ID") + "');}";
            }).GetHtml();
        });
       
        settings.ClientSideEvents.RowDblClick = "function(s,e){s.StartEditRowByKey(s.GetRowKey(e.visibleIndex));}";

        settings.Settings.ShowFilterRowMenu = settings.Settings.ShowFilterRow;
        settings.CommandColumn.ClearFilterButton.Visible = settings.Settings.ShowFilterRow;
        settings.SettingsPager.Position = System.Web.UI.WebControls.PagerPosition.Bottom;
        settings.SettingsPager.FirstPageButton.Visible = true;
        settings.SettingsPager.LastPageButton.Visible = true;
        settings.SettingsPager.PageSizeItemSettings.Visible = true;
        settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };

    });


    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
    
    @grid.BindToCustomData(Model).GetHtml()
}